#!/usr/bin/python
# Author: Anirudh Sanjeev ( anirudh (@t) anirudhsanjeev (d0t) org )
"""
this file runs all the diagnostics and generates the html file from it
"""

import listhelper
from diagnostic import Diagnostic
from jinja import from_string
def generate_results(diags):
    all_results = {}
    cat_id = -1
    for category_name in diags:
        cat_id += 1
        category = diags[category_name]
        category_results = {}
        diag_id = -1
        for diag_name in category:
            diag_id += 1
            diag = category[diag_name]
            ##
            diag.go()
            ##
            res = {} # All the results in a dictionary
            output_string=diag.output
            import re
            
            #re.sub('\\n','</br>'
            res['output'] = output_string
            res['result'] = diag.result
            res['title'] = diag.title
            res['type'] = diag.type
            res['description'] = diag.description
            res['commfile'] = diag.identifier
            res['category'] = category_name
            res['diag_id'] = "%d-%d" % (cat_id, diag_id)
            # Add this test results to the category results
            category_results[diag_name] = res
            # TODO: anything else that we need to add?
        all_results[category_name] = category_results

    # Return all the results
    return all_results

def generate_html(results1):
    template_text = open('template.html').read()
    tpl = from_string(template_text)
    vars = {}
    a1 = {}
    a1["b"] = {'c':'ultimate answer'}
    #print results1
    output_html = tpl.render(result = results1, a=a1)
    outfile = open('output.html', 'w')
    outfile.write(output_html)


if __name__ == '__main__':
    diags = listhelper.get_all_tests()
    results = generate_results(diags)
    generate_html(results)
